package com.example.server.util;

import org.apache.tomcat.util.codec.binary.Base64;
import sun.security.mscapi.CPublicKey;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RsaUtils {

    public static String  publicKey= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuqq+/yHZE2vJmUJDGHqexyqEN\n" +
            "US0Q1idkCtCPpopfZK6/Ox0ODUIalGns5t6NkLIqei1onOMQjg8H8qMh+0JZP1Ek\n" +
            "IjPy+xbv4dl45zGJWcTDzYbuNR54Pjq7CxWOsYEtcQy5TnUSXE+fZeP5W0Cm3kPZ\n" +
            "PRD11+76MjH2xEm3WwIDAQAB";

    public static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAK6qr7/IdkTa8mZQ\n" +
            "kMYep7HKoQ1RLRDWJ2QK0I+mil9krr87HQ4NQhqUaezm3o2Qsip6LWic4xCODwfy\n" +
            "oyH7Qlk/USQiM/L7Fu/h2XjnMYlZxMPNhu41Hng+OrsLFY6xgS1xDLlOdRJcT59l\n" +
            "4/lbQKbeQ9k9EPXX7voyMfbESbdbAgMBAAECgYBps/gKGxmfz9S0IHuMuk6H5PWU\n" +
            "JpUcRE2wWG8hEAtbcYXjSrXppBJmHX0BFjKG/5Mi66+BSLiNczsbdzZMQU9DzKM6\n" +
            "71JvFiBVWfOPuO7W7ItSF2HPro0PA+s7yWlQPM3/wwPdSu+L8WUGG6RZhv9lupDJ\n" +
            "xGzcFt/ydXnvI+cc8QJBAOWf+XVeqiLVrYGNI+z4bg9hv6alRq6TPj9MltAKHGKh\n" +
            "OxPcb9DIFSZNqKypEDVvNZIXWG1qxtYKYQ4f04IDXJkCQQDCurCte5FnqAcqUxKg\n" +
            "WBpfHHtIJKdf8KBULq8bGy9zE+P59Nf+Ib+/6aH7XlbqB1j5+e9Two4gKKkw5ne5\n" +
            "55gTAkEA1kkW/B/ud/meJGL5XsBrvTEEL9jXAD1qfpnTt6tn1+cMgblaI6VEj20z\n" +
            "doWDGcqBXKcqn1lg2wfukMbEk/TdKQJAexm2ExWoqBSq4vC9N6w6j6HbtZGmVZoy\n" +
            "YMVaYRJgfqk2V4+W88q2A0ZY5iWPYEbg0rZTu2mkm2PZAVbQnS9xDQJBALz6I2u5\n" +
            "9JfyvtGC9eLyHB/uQPQfPpqb3ItGH6/zMIdGPMJ63n5ogfRyEkiU6eSUSVjQZZVp\n" +
            "Yr6dFBecbPfb74Y=";

    private static String signature(String plainText) throws  Exception{

        PrivateKey rsaprivate =
                KeyFactory.getInstance("rsa").generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
        // 准备签名对象
        Signature signature = Signature.getInstance("NONEwithRSA");
        // 注册加密私钥
        signature.initSign(rsaprivate);
        // 注册需要签名的文字
        signature.update(plainText.getBytes());
        // 开始签名
        byte[] sign = signature.sign();

        return Base64.encodeBase64String(sign);

    }
    public static boolean verify(String plaintext, String sgin) throws  Exception{
        PublicKey rsaPublic =
                KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));

        Signature signature = Signature.getInstance("NoNEwithRSA");

        signature.initVerify(rsaPublic);

        signature.update(plaintext.getBytes());

        boolean verify = signature.verify(Base64.decodeBase64(sgin));

        return verify;
    }
    public static void main(String[] args) throws Exception {
        String encoded = signature("{\"lastName\":\"success\",\"firstName\":\"success\"}");
        System.out.println(encoded);

        boolean success = verify("{\"lastName\":\"success\",\"firstName\":\"success\"}",encoded);
        System.out.println(success);
    }

}